/*
  Based on the LED bar graph example that comes with 
  the arduino environment. 
  
  THE CIRCUIT:
    * Pin 12: momentary button attached, pulled high externally w/ 10k Ohm resistor
   (LOW = pressed)
    * Pins 2 thru 6: LEDs
  
  THE BEHAVIOR:
  
  When the button is presses the timer will start.
  
  Currently set to be a count up timer, so will light one 
  more led for every timer thershold it reaches.
  
  The range is defined by timerMax, number of segments
  defined by ledCount; 
  
  
  Created: Carlyn Maw / Nov 23, 2010
  Updated: 
 */

//DEFINE STEP ONE: DEBUG AND GLOBAL TIMING VARIABLES
boolean debugFlag = 0;
boolean runFlag = false;

//timing vars
unsigned long lastTime = 0;
unsigned long currentTime = 0;
unsigned long myTime = 0;
//unsigned long timerMax = 3000000; //5 minutes
unsigned long timerMax = 30000; //for debuging

//DEFINE STEP TWO: OUTPUTS & THEIR VARIABLES
const int ledCount = 5;    // the number of LEDs in the bar graph

int ledPins[] = { 
  2, 3, 4, 5, 6 };   // an array of pin numbers to which LEDs are attached

//DEFINE STEP THREE: OUTPUTS & THEIR VARIABLES
//(why didn't I move these into the subfunctions?)
const int buttonOnePin = 12;     
boolean buttonOneState = 0; 
boolean buttonOneTrue = LOW;  //what do I look like when I'm pressed?
boolean lastButtonOneState = 0; 

void setup() {

  //SETUP STEP ONE: attach hardware serial 
  if (debugFlag) Serial.begin(9600);

  //SETUP STEP TWO: Set digital pin mode for outputs and their initial conditions:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT); 
    digitalWrite(ledPins[thisLed], LOW); 
  }

  //SETUP STEP THREE: Set digital pin mode for outputs and their initial conditions:
  pinMode(buttonOnePin, INPUT); 

}

void loop() {

  //LOOP STEP ONE: SAVE, CHECK TIME, RESET CLOCK/RUNFLAG
  int long lastTime = currentTime; 
  int long currentTime = millis();

  if (currentTime != lastTime) {
    myTime = myTime++ ; 
  }

  if (myTime > timerMax) {
    myTime = 0;
    if (debugFlag) Serial.println("myTime Reset");
    runFlag = false;
  }

  //LOOP: STEP TWO, SAVE LAST STATES & POLL THE ENVIRONMENT
  lastButtonOneState = buttonOneState;       
  buttonOneState = digitalRead(buttonOnePin);

  if (buttonOneState != lastButtonOneState && buttonOneState == buttonOneTrue) {
    runFlag ? runFlag=false : runFlag=true;
  }


  //LOOP: STEP THREE, WHO'S LIGHTING UP?
  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(myTime, 0, timerMax, 0, ledCount+1)

  //LOOP: STEP FOUR, LIGHT 'EM IF YA GOTTEM
  if (runFlag == true) {
    // loop over the LED array:
    for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      // if the array element's index is less than ledLevel,
      // turn the pin for this element on/off:
      if (thisLed < ledLevel) {
        //digitalWrite(ledPins[thisLed], LOW); //count down
        digitalWrite(ledPins[thisLed], HIGH); //count up
      } 
      // turn off/on all pins higher than the ledLevel:
      else {
        //digitalWrite(ledPins[thisLed], HIGH);    //count down
        digitalWrite(ledPins[thisLed], LOW);  //count up
      }
    }
  } 
  else {
    //if the runFlag is false, make sure everyone goes out. 
    for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      digitalWrite(ledPins[thisLed], LOW);
    }
  }


}







